import plugin from '../../../lib/plugins/plugin.js'
import fetch from 'node-fetch'
import Config from '../models/Config.js'

export class xyemoji extends plugin {
    constructor() {
        super({
            name: "小月emoji",
            dsc: "小月emoji",
            event: "message",
            priority: 9999,
            rule: [
                {
                    reg: "^#?表情包(.*)",
                    fnc: "emoji",
                },
                {
                    reg: "^#?表情列表",
                    fnc: "emojiList",
                },
            ]
        })
    }

    async emoji(e) {
        if (!Config.getConfig().emoji) return false
        let [, msg] = e.msg.match(this.rule[0].reg) || []
        msg = msg.trim()
        const response = await fetch(`${Config.getConfig().emoji_url}/?list=all`)
        if (!response.ok) return await e.reply("访问 emoji api 失败")
        const list = (await response.json()).folders
        if (!msg) {
            msg = list[Math.floor(Math.random() * list.length)]
        }
        if (list.indexOf(msg) < 0) return true
        const url = `${Config.getConfig().emoji_url}?list=${msg}`
        return await e.reply(segment.image(url))
    }
    async emojiList(e) {
        const response = await fetch(`${Config.getConfig().emoji_url}/?list=all`)
        if (response.ok) {
            const data = await response.json()
            const list = data.folders
            return await e.reply(await this.makeForwardMsg("当前支持的表情包：", list.reduce((acc, cur, index) => {
                const chunkIndex = Math.floor(index / 10)
                if (!acc[chunkIndex]) {
                    acc[chunkIndex] = `${index + 1}.${cur}`
                } else {
                    acc[chunkIndex] += `\n${index + 1}.${cur}`
                }
                return acc
            }, [])))
        }
        return false
    }
    async makeForwardMsg(title, msg, end) {
        let nickname = (this.e.bot ?? Bot).nickname
        let userInfo = {
            user_id: (this.e.bot ?? Bot).uin,
            nickname
        }
        let forwardMsg = [
            {
                ...userInfo,
                message: title
            }
        ]
        for (const message of msg) {
            if (!message) {
                continue
            }
            forwardMsg.push({
                ...userInfo,
                message: message
            })
        }
        if (end) {
            forwardMsg.push({
                ...userInfo,
                message: end
            })
        }
        if (this.e.group?.makeForwardMsg) {
            forwardMsg = await this.e.group.makeForwardMsg(forwardMsg)
        } else if (this.e?.friend?.makeForwardMsg) {
            forwardMsg = await this.e.friend.makeForwardMsg(forwardMsg)
        } else {
            return msg.join('\n')
        }
        let dec = '表情列表'
        if (typeof (forwardMsg.data) === 'object') {
            let detail = forwardMsg.data?.meta?.detail
            if (detail) {
                detail.news = [{ text: dec }]
            }
        } else {
            forwardMsg.data = forwardMsg.data
                .replace(/\n/g, '')
                .replace(/<title color="#777777" size="26">(.+?)<\/title>/g, '___')
                .replace(/___+/, `<title color="#777777" size="26">${dec}</title>`)
        }
        return forwardMsg
    }
}